package com.fecean.aabbc.netty.server.init;

import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ClientChannelCache
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-02-25 13:43
 * @blame Team
 */
@Component
public class ClientChannel {

    private static ConcurrentHashMap<String, Channel> clientChannelMap = new ConcurrentHashMap<>();

    public void saveClientChannel(String clientKey, Channel clientChannel){
        clientChannelMap.remove(clientKey);
        clientChannelMap.put(clientKey, clientChannel);
    }

    public Channel getClientChannel(String clientKey){
        return clientChannelMap.get(clientKey);
    }

    public void removeClientChannel(String clientKey){
        clientChannelMap.remove(clientKey);
    }

    public void removeClientChannel(Channel clientChannel){
        clientChannelMap.entrySet().removeIf(entry -> entry.getValue() == clientChannel);
    }

    public int getSize(){
        return clientChannelMap.size();
    }
}
